package com.itheima.service.impl;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/*
    业务层实现类
 */
public class StudentServiceImpl implements StudentService {



    @Override
    public List<Student> selectAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectAll();
        sqlSession.close();
        return students;
    }

    @Override
    public Student selectById(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(id);
        sqlSession.close();
        return student;
    }

    @Override
    public Integer insert(Student stu) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer insert = mapper.insert(stu);
        sqlSession.close();
        return insert;
    }

    @Override
    public Integer update(Student stu) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer insert = mapper.update(stu);
        sqlSession.close();
        return insert;
    }

    @Override
    public Integer delete(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer insert = mapper.delete(id);
        sqlSession.close();
        return insert;
    }

    @Override
    public Student selectCondition(Student stu) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectCondition(stu);

        return student;
    }

    @Override
    public List<Student> selectByIds(List<Integer> ids) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByIds(ids);
        return students;
    }
}
